package com.yanqu.road.server.manger;

import com.yanqu.road.dao.impl.servercenter.ServerAutoOpenConfigDaoImpl;
import com.yanqu.road.entity.servercenter.ServerAutoOpenConfig;
import com.yanqu.road.server.TempMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AutoOpenServerMgr extends TempMgr {

	private static final Logger logger = LogManager.getLogger(AutoOpenServerMgr.class.getName());

	private static Map<Integer, ServerAutoOpenConfig> autoOpenConfigMap = new ConcurrentHashMap<>();

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadData() {
		autoOpenConfigMap = new ServerAutoOpenConfigDaoImpl().getServerAutoOpenConfig();
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	public static Map<Integer, ServerAutoOpenConfig> getAutoOpenConfigMap() {
		return autoOpenConfigMap;
	}

	public static int getAutoOpenRoleLevel(int channelId){
		int level = 2;
		ServerAutoOpenConfig serverAutoOpenConfig = autoOpenConfigMap.get(channelId);
		if(serverAutoOpenConfig != null){
			level = serverAutoOpenConfig.getLevel();
		}
		return level;
	}

	public static int getAutoOpenRoleLimitType(int channelId){
		int type = 1;
		ServerAutoOpenConfig serverAutoOpenConfig = autoOpenConfigMap.get(channelId);
		if(serverAutoOpenConfig != null){
			type = serverAutoOpenConfig.getType();
		}
		return type;
	}

	public static void setAutoOpenConfig(int channelId, ServerAutoOpenConfig serverAutoOpenConfig){
		autoOpenConfigMap.put(channelId, serverAutoOpenConfig);
	}

	public static void setAutoOpenConfig(int channelId, int type, int level, String areaSplitFlow){
		ServerAutoOpenConfig serverAutoOpenConfig = initServerAutoOpenConfig(channelId, type, level, areaSplitFlow);
		setAutoOpenConfig(channelId, serverAutoOpenConfig);
	}

	public static ServerAutoOpenConfig initServerAutoOpenConfig(int channelId, int type, int level, String areaSplitFlow) {
		ServerAutoOpenConfig serverAutoOpenConfig = new ServerAutoOpenConfig();
		serverAutoOpenConfig.setChannelId(channelId);
		serverAutoOpenConfig.setType(type);
		serverAutoOpenConfig.setLevel(level);
		serverAutoOpenConfig.setAreaSplitFlow(areaSplitFlow);
		serverAutoOpenConfig.parseAreaSplitFlow();
		return serverAutoOpenConfig;
	}
}
